#include "GoldCounterLayer.h"
#include "Counter.h"
using namespace cocos2d;

GoldCounterLayer::GoldCounterLayer()
	:_number(0)
{
}


GoldCounterLayer::~GoldCounterLayer()
{
}

bool GoldCounterLayer::init(int number)
{
	int fontSize=16;
	CCLabelTTF* goldLabel=CCLabelTTF::create("金币:","Thonburi",fontSize);
	addChild(goldLabel);
	CCSize goldLabelSize=goldLabel->getContentSize();
	for(int i=0;i<6;i++){
		const int count=10;
		CCArray* presenters=CCArray::createWithCapacity(count);
		for(int j=0;j<count;j++){
			CCLabelTTF* label=CCLabelTTF::create(CCString::createWithFormat("%d",j)->getCString(),"Thonburi",fontSize);
			presenters->addObject(label);
		}
		Counter* counter=Counter::create(presenters);
		counter->setPosition(goldLabelSize.width*0.8+fontSize*0.75*i,0);
		addChild(counter,0,i);
	}
	setNumber(number);
	return true;
}

GoldCounterLayer* GoldCounterLayer::create(int number/*=0*/)
{
	GoldCounterLayer* pRet=new GoldCounterLayer;
	pRet->init(number);
	pRet->autorelease();
	return pRet;
}

void GoldCounterLayer::setNumber(int number,int ceiling /*= 999999*/)
{
	_number=number<0?0:number>ceiling?ceiling:number;
	for(int i = 0 ;i < 6;i++){
		Counter* counter = static_cast<Counter*>(getChildByTag(i));
		int digit = _number/(int)(pow(10.0, 6-i-1))%10; //分离数字
		counter->setDigit(digit);
	}
}

